package com.learning.spring.cloud.api;

import com.learning.spring.cloud.domain.service.ProductService;
import com.learning.spring.cloud.dto.ProductDto;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Log
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService service;

    @PostMapping(value = "/create")
    public ProductDto create(@RequestBody ProductDto dto) {
        return service.save(dto);
    }

    @GetMapping("/findAll")
    public List<ProductDto> findAll() {
        return service.findAll();
    }

    @GetMapping("/find/{code}")
    public ProductDto find(@PathVariable String code) {
        log.info("receive request:" + code);
        return service.findByCode(code);
    }
}
